package com.qxzn.module.home.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.ContactBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.HomeModel;
import com.qxzn.app.page.util.ItemTouchHelperCallback;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.Constant;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.home.R;
import com.qxzn.module.home.R2;
import com.qxzn.module.home.adapter.CareRemindAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CareRemindActivity extends BaseActivity implements MyInterface.GetContactListener, OnItemClickListener {
    @BindView(R2.id.lrv_care_remind)
    LRecyclerView lrvCareRemind;

    private CareRemindAdapter mCareRemindAdapter;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private ItemTouchHelper mItemTouchHelper;
    private ItemTouchHelperCallback mCallback;
    private HomeModel homeModel;

    @Override
    protected int initLayout() {
        return R.layout.activity_care_remind;
    }

    @Override
    protected void initView() {
        lrvCareRemind.setLayoutManager(new LinearLayoutManager(this));
        mCareRemindAdapter = new CareRemindAdapter(this);
        mCareRemindAdapter.setRemindClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CareRemindActivity.this, UserRemindListActivity.class).putExtra(Constant.PutExtraKey.USERNAME, v.getTag() + ""));
            }
        });
        mCareRemindAdapter.setMsgClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContactBean item = (ContactBean) v.getTag();
                if (item == null) {
                    return;
                }
                startActivity(new Intent().setComponent(new ComponentName(CareRemindActivity.this, "com.qxzn.module.im.activity.ChatActivity"))
                        .putExtra("receiver", item.getUsername()).putExtra("nickname", item.getUserresult() == null ? "" : item.getUserresult().getNiname()));
            }
        });
        mCareRemindAdapter.setDataList(new ArrayList<ContactBean>());
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mCareRemindAdapter);
        mCareRemindAdapter.setLRecyclerViewAdapter(mLRecyclerViewAdapter);
        lrvCareRemind.setAdapter(mLRecyclerViewAdapter);

        mCallback = new ItemTouchHelperCallback(mCareRemindAdapter, mLRecyclerViewAdapter);
        mItemTouchHelper = new ItemTouchHelper(mCallback);
        mItemTouchHelper.attachToRecyclerView(lrvCareRemind);

//        DividerDecoration divider = new DividerDecoration.Builder(this)
//                .setHeight(R.dimen.default_divider_height)
//                .setPadding(R.dimen.default_divider_padding)
//                .setColorResource(R.color.split)
//                .build();
//        lrvCareRemind.addItemDecoration(divider);
        lrvCareRemind.setHasFixedSize(true);
        //禁用下拉刷新功能
        lrvCareRemind.setPullRefreshEnabled(false);
        //禁用自动加载更多功能
        lrvCareRemind.setLoadMoreEnabled(false);
        mLRecyclerViewAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("关怀提醒");
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("管理");
        tv_right.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        homeModel = new HomeModel(this);
        initCareRemind();
    }

    private void initCareRemind() {
        JSONObject params = new JSONObject();
        params.put("device_id", SharePreferenceUtil.getDeviceId(this));
        params.put("username", SharePreferenceUtil.getUserName(this));
        homeModel.setGetContactListener(this);
        homeModel.getContact(params);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.tv_right) {
            AppUtil.showLog(mCareRemindAdapter.getDataList().toString());
            if (mCallback.isLongPressDragEnabled()) {
                tv_right.setText("管理");
                mCallback.setDragEnabled(false);
                mCareRemindAdapter.setDragEnabled(false);
            } else {
                tv_right.setText("完成");
                mCallback.setDragEnabled(true);
                mCareRemindAdapter.setDragEnabled(true);
            }
            mCareRemindAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        homeModel.removeListener();
        super.onDestroy();
    }

    @Override
    public void onGetContactsSuccess(BaseBean<List<ContactBean>> baseBean) {
        if (baseBean != null && baseBean.getData() != null) {
            mCareRemindAdapter.setDataList(baseBean.getData());
            mLRecyclerViewAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onGetContactsFailure() {

    }

    @Override
    public void onItemClick(View view, int position) {
        if (mCallback.isLongPressDragEnabled()) {
            return;
        }
        ContactBean bean = mCareRemindAdapter.getDataList().get(position);
        startActivity(new Intent(this, CareRemindDetailActivity.class).putExtra("bean", bean));
    }
}
